//
//  AIRegisterController.m
//  MiaMind
//
//  Created by Vill on 2023/12/11.
//

#import "AIRegisterController.h"
#import "AISoulmateController.h"    // soulmateView
#import "AIHomeMainController.h"
#import "AIChatManager.h"

@interface AIRegisterController ()

@property (nonatomic, strong) AISoulmateController *soulMateVc;
@property (nonatomic, strong) AIHomeMainController *homeView;

@end

@implementation AIRegisterController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [AIUnityInstance.instance pause:NO];
    [AIUnityInstance.instance showUnityView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [AIUnityInstance.instance pause:YES];
    [AIUnityInstance.instance showNativeView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.soulMateVc = [[AISoulmateController alloc] init];
    self.homeView = [[AIHomeMainController alloc] init];
    WeakSelf(self);
    self.soulMateVc.soulmateDoneBlock = ^{
        if (weakself.soulMateVc.view){
            [weakself.soulMateVc.view removeFromSuperview];
            weakself.soulMateVc.view = nil;
        }
        [AIUnityInstance.instance.unityAppController.rootView addSubview:weakself.homeView.view];
    };
    [AIUnityInstance.instance initUnityWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"InitSDK" UTF8String] message:[@"1" UTF8String]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        AIUserInfoModel *userInfo = [AIUserInfoManager shareManager].userInfo;
        //是否注册，开启tcp连接的时候，第一个消息要带上
        if (userInfo.regstier == NO){
            [AIUnityInstance.instance.unityAppController.rootView addSubview:self.homeView.view];
        }else{
            [AIUnityInstance.instance.unityAppController.rootView addSubview:self.soulMateVc.view];
        }
        [self connectSocket];
    });
    
    // 删除或者退出账户
    NTFAdd(DeleteOrLogoutNotification, @selector(deleteOrLogOutNoti:));
}

// 移除所有子控件
- (void)deleteOrLogOutNoti:(NSNotification *)noti{
    for (UIView *view in [AIUnityInstance.instance.unityAppController.rootView subviews]){
        [view removeFromSuperview];
    }
}

// 连接聊天
- (void)connectSocket{
    // 先断开连接
    [[AIChatManager shareInstance] executeDisconnectServer];
    // 连接tcp
    [[AIChatManager shareInstance] connectServerHost];
    AIChatModel *model = [AIChatModel new];
    model.token = [AIUserInfoManager shareManager].userInfo.token;
//    AIUserInfoModel *userInfo = [AIUserInfoManager shareManager].userInfo;
//    NSLog(@"userinfo----- %@",[userInfo mj_JSONObject]);
    model.chatType = @"LOGIN";
    AIChatContentModel *contentModel = [AIChatContentModel new];
    //是否注册，开启tcp连接的时候，第一个消息要带上
    contentModel.isRegister = [AIUserInfoManager shareManager].userInfo.regstier;
    // 今天是否第一次登录
    if ([[NSString getCurrentUTCDateString] isFirstLoginToday]) {
        contentModel.dayFirstLogin = YES;
    } else {
        contentModel.dayFirstLogin = NO;
    }
    model.data = contentModel;
    [[AIChatManager shareInstance] sendMessage:model timeOut:-1 tag:1];
}



@end


